const app = getApp(),url = app.globalData.url;
var WxParse = require('../wxParse/wxParse.js');
Page({

  data: {
    url : app.globalData.url,
    iscoll:'',
    isshenqing:2,//自定义  2  未申请  1  已申请
    elAppls: '', // 用户审核状态
    approval: '', // 简历审核状态
    jlzt: '',     // 简历启用状态
    readmore: {
      status: true,      
      tip: '查看更多',
      rrow:'https://res.tuoluohuodong.com/green_bottom_rrow.png'
    }
  },
  toggle() {
    const status = this.data.readmore.status
    
    this.setData({
    readmore: {
    status: !status,
    
    tip: status ? '收起' : '查看更多',
    
    rrow: status ? 'https://res.tuoluohuodong.com/green_top_rrow.png' : 'https://res.tuoluohuodong.com/green_bottom_rrow.png'
    
    }
    
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var zid = options.id
		var uid = wx.getStorageSync('user_massage')
    that.setData({
      collid:options.collid,
      // isCollection:options.isColl
    })
    wx.request({
      url:url+'position/getPosition',
      method: "GET",
      data:{
        position_id:options.id,
        userUid:uid.userUid ? uid.userUid : '' ,
      },
      success:function(res){
        console.log("职位详情",res);
        var ret = res.data
        if(ret.code == 200){
          // 企业详情
          that.qydetail(ret.data.enterpriseUid)
          var s = ret.data.welfareRemark
          var ss = s.split(",");
          that.setData({
            detail:ret.data,
            ss:ss
          })
          that.setData({
            options:ret.data,
          })
          var content = ret.data.positionInfo
          if(content != null){
            WxParse.wxParse('article', 'html',content, that, 5);
          }

        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
    // 收藏列表 判断是不是收藏了 此 id 职位
    var user_massage = wx.getStorageSync('user_massage')
    wx.request({
      header: {
        'content-type': 'application/json' // 默认值
      },
      url:url+'myCollection/getMyCollectionList',
      method: "GET",
      data:{user_uid:user_massage.userUid,pageNum:1,pageSize:9999999,},
      success:function(res){
        // console.log("收藏列表",res);
        var ret = res.data
        if(ret.code == 200){
          var arr = ret.data.content
          for(let i of arr){
            // console.log(i)
            if(zid == i.positionId){
              that.setData({iscoll:1, collid:i.id})
              break;
            }else{
              that.setData({iscoll:2})
            }
          }
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
    //申请职位  判断是不是申请过
    wx.request({
      url:url + 'apply/getAllApplyPosition',
      header:{'context-type': 'application/json',},
      method: 'GET',
      data:{user_uid:user_massage.userUid,pageNum:1,pageSize:9999999,},
      success:function(res){
        console.log("申请职位",res);
        var ret = res.data
        if(ret.code == 200){
          if(ret.data.content != ''){
            var arr = ret.data.content
            for(let i of arr){
              console.log(i)
              if(zid == i.positionId){
                that.setData({isshenqing:1})
                break;
              }else{
                that.setData({isshenqing:2})
              }
            }
          }else{
            // wx.showToast({
            //   title: '',
            //   icon:'none'
            // })
          }
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })

    //
    this.getCVBaseInfo()
  },
  getCVBaseInfo(){
    let _this = this
    var userMsg = wx.getStorageSync('user_massage')
    wx.request({
      url: url + 'cvBaseInfo/getCVBaseInfoByUserUid?user_uid='+ userMsg.userUid,
      method: "GET",
      success(res){
        var ret = res.data
        if(ret.code != 200 || !ret.data){
          wx.showToast({
            title: '请添加简历',
            icon:'none'
          })
          return
        }
        _this.setData({
          elAppls: ret.data.elAppls,
          jlzt: ret.data.jlzt,
          approval: ret.data.approval
        })
      }
    })
  },
  qydetail(enterpriseUid){
    var that =  this
    wx.request({
      url:url+'enterprise/getEnterpriseInfoByUid',//getEnterpriseInfo
      method: "GET",
      data:{enterprise_uid:enterpriseUid},//enterprise_uid
      success:function(res){
        console.log("详情 ",res);
        var ret = res.data
        if(ret.code == 200){
          that.setData({
            qiye:ret.data,
          })
          var content = ret.data.introduction//enterpriseInfo 企业信息   introduction 企业简介
          WxParse.wxParse('yqdetail', 'html',content, that, 5);
        }
      }
    })
  },
  // 是否收藏
  buncloo(e){
    var user_massage = wx.getStorageSync('user_massage')

    if(user_massage == ''|| user_massage == null){
      wx.showToast({
        title:'请前往登录~~~',
        icon:'none'
      })
      return false
    }
    let {elAppls, jlzt} = this.data
    if (!elAppls || elAppls != 2) {
      wx.showToast({ 
        title: '用户未通过审核，不能申请职位', 
        icon:'none'
      })
      return
    }
    if (jlzt != 1) {
      wx.showToast({ 
        title: '简历异常，请完善简历', 
        icon:'none'
      })
      return
    }
    var that = this
    var sts = e.currentTarget.dataset.sts
    var positionId = e.currentTarget.dataset.id
    var qid = e.currentTarget.dataset.qid
    wx.request({
      url:url+'myCollection/addToMyCollection',//myCollection/addToMyCollection
      method: "POST",
      data:{
        enterpriseUid:qid,
        positionId:positionId,
        userUid: user_massage.userUid,
        isCollection:1,
      },
      success:function(res){
        console.log("收藏",res);
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title: '收藏成功',
            icon:'none'
          })
          that.setData({
            iscoll:sts,collid:ret.data.id
          })
        }else{
          wx.showToast({
            title: '收藏失败',
            icon:'none'
          })
        }
      }
    })
  },
  quxiao(e){
    var that = this
    var sts = e.currentTarget.dataset.sts
    var collid = that.data.collid//收藏的ID
    console.log(collid)
    if(collid == undefined){
      wx.showToast({
        title: '请前往收藏列表~',
        icon:'none',
      })
      return false
    }
    wx.request({
      url:url+'myCollection/cancelMyCollection?collection_id='+Number(collid),//cancelMyCollection
      method: "DELETE",
      data:{
        
      },
      success:function(res){
        console.log("取消收藏",res);
        var ret = res.data
        if(ret.code == 200){
          that.setData({
            iscoll:sts
          })
          wx.showToast({
            title: '已取消收藏',
            icon:'none'
          })
        }else{
          wx.showToast({
            title: '操作失败',
            icon:'none'
          })
        }
      }
    })
  },
  // 申请职位
  sq(e){
    var that=  this
    var uid = wx.getStorageSync('user_massage')
    if(!uid){
      wx.showToast({
        title:'请前往登录~~~',
        icon:'none'
      })
      return false
    }

    let {elAppls, jlzt, approval} = this.data
    if (!elAppls || elAppls != 2) {
      wx.showToast({ 
        title: '用户未通过审核，不能申请职位', 
        icon:'none'
      })
      return
    }
    if (!approval || approval != 2) {
      wx.showToast({ 
        title: '简历未通过审核，不能申请职位', 
        icon:'none'
      })
      return
    }
    if (jlzt != 1) {
      wx.showToast({ 
        title: '简历异常，请完善简历', 
        icon:'none'
      })
      return
    }
    
    var pid = e.currentTarget.dataset.id
    var enterpriseUid = e.currentTarget.dataset.enterpriseuid
    wx.showModal({
      title: '提示',
      content: '确认申请该职位？',
      success (res) {
        if (res.confirm) {
          wx.showLoading({ title: '加载中...'})
          wx.request({
            url:url+'apply/position',
            method: "POST",
            data:{
              "enterpriseUid": enterpriseUid,
              "positionId":pid,
              "userUid": uid.userUid,
              'isApply':'1',//是否申请职位,1:是， 2：否
              'applyState':'5',
              laiYuan: '小程序',
            },
            // {"positionId":pid, "userUid": uid.userUid, "enterpriseUid": enterpriseUid},
            success:function(res){
              wx.hideLoading()
              var ret = res.data
              if(ret.code == 200){
                wx.showToast({
                  title: '申请成功',
                  icon:'none'
                })
                that.setData({isshenqing:1})
              }else{
                that.setData({isshenqing:2})
                wx.showToast({
                  title: '请前往个人中心创建在线简历',
                  icon:'none'
                })
              }
            }
          })
        } else if (res.cancel) {
          wx.hideLoading()
          console.log('用户点击取消')
        }
      }
    })
  },
  prisedetail(e){
    let pid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/HomePage/prisedetail/prisedetail?id='+pid,
    })
  },

})